﻿using Autodesk.AutoCAD.DatabaseServices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace AutoCadConvert
{
    public class Window:Cube
    {
        public enum WindowType
        {
            Base = 1,//基础窗
            French,//落地窗
            Floating,//飘窗
        }
        public Window()
        {
            Height = 1400;
        }
        public WindowType eWindowType = WindowType.Base;//类型
        public double HeightToFloor = 500;//离地高度
        public double Depth = 0;//深度
        public bool FillLight = false;//是否补光
        public bool bRotated = false;

        public override IEnumerable<XElement> ToXml()
        {
            return base.ToXml().Concat(new XElement[] {
                new XElement("WindowType",(int)eWindowType),
                new XElement("HeightToFloor",HeightToFloor*Unit),
                new XElement("Depth",Depth*Unit),
                new XElement("FillLight",FillLight),
                new XElement("bRotated",bRotated),
            }).ToArray();
        }
    }
    public class Windows : Collection
    {
        public List<Window> m_Window = new List<Window>();
        public Windows(House house):base(house){}
        public override void AjustLineAndPoint()
        {
            //位置矫正
            Point center = m_house.Item<Points>().m_Center;
            foreach (BspObject obj in m_house.objList)
            {
                Collection collect = obj as Collection;
                if (collect != null)
                {
                    foreach (BspObject item in collect.m_EntityList)
                    {
                        if (item is Block)
                        {
                            Block bref = item as Block;
                            if (bref.m_Block.GetValueAttributesInBlock("图例分类") == "硬装" 
                                && bref.m_Block.GetValueAttributesInBlock("图例编码") == "chuanghu")
                            {
                                List<Point> ptls = new List<Point>();
                                Point basePt = new Point(bref.m_Block.Position);
                                Block.RecurBlockReference(bref.m_Block, bref.m_Block.ScaleFactors, new Point(bref.m_Block.Position), center, ref ptls);//解析数据
                                if (ptls.Count == 0) continue;
                                double dMaxX = ptls.Max(o => o.X), dMaxY = ptls.Max(o => o.Y), dMinX = ptls.Min(o => o.X), dMinY = ptls.Min(o => o.Y);

                                //属性字段
                                bool bFillLight = false, bRotated = false;
                                Window.WindowType wType = Window.WindowType.Base;
                                double dHeight, dHeightToFloor, dDeep;
                                double.TryParse(bref.m_Block.GetValueAttributesInBlock("高度"), out dHeight);
                                double.TryParse(bref.m_Block.GetValueAttributesInBlock("离地高度"), out dHeightToFloor);
                                Enum.TryParse(bref.m_Block.GetValueAttributesInBlock("窗户类型"), out wType);
                                double.TryParse(bref.m_Block.GetValueAttributesInBlock("深度"), out dDeep);
                                Boolean.TryParse(bref.m_Block.GetValueAttributesInBlock("是否补光"), out bFillLight);
                                Boolean.TryParse(bref.m_Block.GetValueAttributesInBlock("是否旋转"), out bRotated);

                                m_Window.Add(new Window()
                                {
                                    Width = dMaxY - dMinY,
                                    Thickness = dMaxX - dMinX,
                                    Pos = (basePt - center),
                                    Rotation = bref.m_Block.Rotation / Math.PI * 180,
                                    Height = dHeight==0?1400:dHeight,
                                    eWindowType = wType,
                                    HeightToFloor = dHeightToFloor,
                                    Depth = dDeep,
                                    FillLight = bFillLight,
                                    bRotated = bRotated
                                });
                            }
                        }
                    }
                }
            }
        }
        public override IEnumerable<XElement> ToXml()
        {
            int idx = 0;
            foreach (Window wn in m_Window)
                yield return new XElement("_" + idx++, wn.ToXml());
        }
    }
}
